﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using MyFinanceManager.Controls;

namespace MyFinanceManager
{
    public partial class ucDate : FiringUserControl
    {
        public DateTime Value
        {
            get
            {
                int month = this.upAndDowner3.Value;
                month += this.upAndDowner2.Value * 10;
                int day = this.upAndDowner1.Value;
                day += this.upAndDowner0.Value * 10;
                int year = this.upAndDowner5.Value;
                year += (this.upAndDowner4.Value * 10);
                year += 2000;
                try
                {
                    return new DateTime(year, month, day);
                }
                catch (Exception)
                {
                    return new DateTime();
                }
            }
        }

        public ucDate()
        {
            InitializeComponent();

            this.upAndDowner1.Value = DateTime.Now.Day - ((int)(DateTime.Now.Day / 10)) * 10;
            this.upAndDowner3.Value = DateTime.Now.Month - ((int)(DateTime.Now.Month / 10)) * 10;
            this.upAndDowner5.Value = (DateTime.Now.Year - 2000) - ((int)((DateTime.Now.Year - 2000) / 10)) * 10;

            if (DateTime.Now.Day > 10)
                this.upAndDowner0.Value = (int)(DateTime.Now.Day / 10);
            if (DateTime.Now.Month > 10)
                this.upAndDowner2.Value = (int)(DateTime.Now.Month / 10);
            if (DateTime.Now.Year > 2010)
                this.upAndDowner4.Value = (int)((DateTime.Now.Year - 2000) / 10);

            this.upAndDowner0.ValueChanged += new EventHandler(upAndDowner_ValueChanged);
            this.upAndDowner1.ValueChanged += new EventHandler(upAndDowner_ValueChanged);
            this.upAndDowner2.ValueChanged += new EventHandler(upAndDowner_ValueChanged);
            this.upAndDowner3.ValueChanged += new EventHandler(upAndDowner_ValueChanged);
            this.upAndDowner4.ValueChanged += new EventHandler(upAndDowner_ValueChanged);
            this.upAndDowner5.ValueChanged += new EventHandler(upAndDowner_ValueChanged);
        }

        void upAndDowner_ValueChanged(object sender, EventArgs e)
        {
            FireNewValueAvailable(this.Value.ToLongDateString());
        }

    }
}
